package thread_01;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class myThreadPool {
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
    //    初始化线程池
    public myThreadPool(int n){
        for (int i = 0; i < n; i++) {
//                创建线程
            Thread t = new Thread(() ->{
//                    把他放入队列中
                try {
                    while(true){
                        Runnable runnable = queue.take();
                        runnable.run();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            t.start();
        }
    }
    //    把任务添加到线程池中
    void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
public class Demo36 {
    public static void main(String[] args) throws InterruptedException {
        myThreadPool threadPool = new myThreadPool(10);
        for (int i = 0; i < 10000; i++) {
            int finalI = i;
            threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello "+ finalI + Thread.currentThread().getName());
                }
            });
        }
    }
}
